const eleMap = new WeakMap()
/**
 * @desc 元素高度切换
 * @param { ele } DomObject dom元素
 * @param { visible } boolean 显示还是隐藏： true 设置高度为 0，false恢复高度
 */
export const eleHightToggle = (ele, visible) => {
  if (!ele) return
  if (visible) {
    const rect = ele.getBoundingClientRect()
    const redraw = !ele.style.height || ele.style.height === 'auto'
    ele.style.height = parseInt(rect.height) + 'px'
    // getBoundingClientRect 触发重绘，否则第一次点击没有动画
    eleMap.set(ele, redraw ? ele.getBoundingClientRect() : rect)
  }
  let style = {
    overflow: 'hidden',
    transition: 'all 0.8s',
  }
  if (visible) {
    style.height = 0
  } else {
    const rect = eleMap.get(ele)
    style.height = parseInt(rect.height) + 'px'
  }
  Object.entries(style).forEach(([k, v]) => {
    ele.style[k] = v
  })
}
